<?php
    require('retention.doc.php');
    //课程预约
	$app->post(
		'/retention',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['curriculumid']) && isset($data['memberid']) && isset($data['dt'])){
				$curriculum=$db2->select(1,'tb_curriculum_snap',array('[><]tb_order'=>array('idcode')),'*',array('tb_curriculum_snap.tbid'=>$data['curriculumid'],'tb_order.userid'=>$data['memberid'],'tb_order.usertype'=>1,'tb_order.state'=>2));
				if(isset($curriculum['tbid']) && strtotime($data['dt'])<strtotime("+31 day") && strtotime($data['dt'])>strtotime("now")){
					if($db2->select(2,'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')),'*',array('tb_curriculum_snap.coach_id'=>$curriculum["coach_id"],'tb_reserve.dt'=>$data['dt'],'tb_reserve.state[!]'=>2))==0){
						$reserve=$db2->select(2,'tb_reserve','*',array('curriculum_id'=>$data['curriculumid'],array('state[!]'=>array(2))));
						if($reserve<$curriculum['number']){
							$db2->insert('tb_reserve',array('curriculum_id'=>$data['curriculumid'],'member_id'=>$data['memberid'],'dt'=>$data['dt']));
							/*if($reserve+1==$curriculum['sku_number']){
								$db2->update('tb_order',array('state'=>3),array('idcode'=>$curriculum['idcode']));
							}*/
						}else{
							$echo['error'] = "没有剩余课程数目可以预约";
						}
					}else{
						$echo['error'] = "教练在该时间不在可预约状态";
					}
				}else{
					$echo['error'] = "课程不存在或不在可预约状态";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//取消预约
	$app->post(
		'/retention/undo',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['reserveid']) && isset($data['userid']) && isset($data['usertype'])){
				if($data['usertype']==1){
					$reserve=$db2->select(1,'tb_reserve','*',array('tbid'=>$data['reserveid'],'state'=>0,'member_id'=>$data['userid']));
				}else{
					$reserve=$db2->select(1,'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')),'*',array('tb_reserve.tbid'=>$data['reserveid'],'tb_reserve.state'=>0,'tb_curriculum_snap.coach_id'=>$data['userid']));
				}
				if($reserve['tbid']!=''){
					$curriculum=$db2->select(1,'tb_curriculum_snap','*',array('tbid'=>$reserve['curriculum_id']));
					/*if($db2->select(2,'tb_reserve','*',array('curriculum_id'=>$reserve['curriculum_id'],array('state[!]'=>array(2))))==$curriculum['sku_number']){
						$db2->update('tb_order',array('state'=>2),array('idcode'=>$curriculum['idcode']));
					}*/
					$db2->update('tb_reserve',array('state'=>2),array('tbid'=>$data['reserveid']));
				}else{
					$echo['error'] = "预约记录不存在，或不在可取消状态";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//教练确认预约
	$app->post(
		'/retention/check',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['reserveid']) && isset($data['coachid'])){
				$reserve=$db2->select(2,'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')),'*',array('tb_reserve.tbid'=>$data['reserveid'],'tb_reserve.state'=>0,'tb_curriculum_snap.coach_id'=>$data['coachid']));
				if($reserve==1){
					$db2->update('tb_reserve',array('state'=>3),array('tbid'=>$data['reserveid']));
				}else{
					$echo['error'] = "预约记录不存在，或不在可确认状态";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//教练确认开始上课
	$app->post(
		'/retention/start',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['reserveid']) && isset($data['coachid'])){
				$reserve=$db2->select(2,'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')),'*',array('tb_reserve.tbid'=>$data['reserveid'],'tb_reserve.state'=>1,'tb_curriculum_snap.coach_id'=>$data['coachid']));
				if($reserve==1){
					$db2->update('tb_reserve',array('state'=>3),array('tbid'=>$data['reserveid']));
				}else{
					$echo['error'] = "预约记录不存在，或不在可开始上课状态";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//确认上课结束
	$app->post(
		'/retention/end',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['reserveid']) && isset($data['userid']) && isset($data['usertype'])){
				if($data['usertype']==1){
					$reserve=$db2->select(1,'tb_reserve','*',array('tbid'=>$data['reserveid'],'state'=>array(3,4),'member_id'=>$data['userid']));
					if($reserve['state']==4){
						$state=6;
					}else{
						$state=5;
					}
				}else{
					$reserve=$db2->select(1,'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')),'*',array('tb_reserve.tbid'=>$data['reserveid'],'tb_reserve.state'=>array(3,5),'tb_curriculum_snap.coach_id'=>$data['userid']));
					if($reserve['state']==4){
						$state=6;
					}else{
						$state=4;
					}
				}
				if(isset($reserve['tbid'])){
					$db2->update('tb_reserve',array('state'=>$state),array('tbid'=>$data['reserveid']));
					if($state==6){
						$reserve=$db2->select(2,'tb_reserve','*',array('curriculum_id'=>$data['curriculumid'],array('state'=>array(6,7))));
						$curriculum=$db2->select(1,'tb_curriculum_snap','*',array('tb_curriculum_snap.tbid'=>$reserve['curriculum_id']));
						if($reserve>=$curriculum['number']){
							$db2->update('tb_order',array('state'=>3),array('idcode'=>$db2->select(3,'tb_curriculum_snap','idcode',array("tbid"=>$reserve['curriculum_id']))));
						}
					}
				}else{
					$echo['error'] = "预约记录不存在，或不在可确认结束状态";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//教练排期
	$app->get(
		'/retention/coach/layout',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['curriculumid'])){
				$echo['data']=$db2->select(3,'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')),'dt',array('tb_curriculum_snap.coach_id'=>$db2->select(3,'tb_curriculum_snap','coach_id',array('tbid'=>$data['curriculumid'])),'tb_reserve.state[!]'=>2,'#date_format(dt,"%Y-%m-%d")[>]#1'=>'DATE_ADD(CURDATE(),INTERVAL 1 day)','#date_format(dt,"%Y-%m-%d")[<=]#1'=>'DATE_ADD(CURDATE(),INTERVAL 30 day)'));
				if(!is_array($echo['data']) && !empty($echo['data'])){
					$echo['data']=array($echo['data']);
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//预约列表
	$app->get(
		'/retention/list',
		function() use($app, $db2 ,$curriculumtype){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['userid']) && isset($data['usertype']) && isset($data['type']) && isset($data['from']) && isset($data['to'])){
				if($data['usertype']==1){
					$where=array('member_id'=>$data['userid']);
				}else{
					$where=array('tb_curriculum_snap.coach_id'=>$data['userid']);
				}
				$where['order']=array('tb_reserve.dt desc');
				if($data['type']==1){
					$where['tb_reserve.state']=array(0);
				}elseif($data['type']==2){
					$where['tb_reserve.state']=array(3,4,5);
				}elseif($data['type']==3){
					$where['tb_reserve.state']=array(2,6,7);
				}elseif($data['type']==4){
					$where['tb_reserve.state']=array(0,3,4,5);
				}
				$echo['count'] = $db2->select(2, 'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')), '*', $where);
				$where['limit'] = array($data['from'], $data['to']);
				$reserve=$db2->select(0,'tb_reserve',array('[><]tb_curriculum_snap'=>array('curriculum_id'=>'tbid')),'tb_reserve.tbid,tb_curriculum_snap.title,tb_curriculum_snap.intro,('.$db2->select(4,'tb_coach','coach_id',array('#tb_coach.tbid'=>'tb_curriculum_snap.coach_id')).')[coachid],('.$db2->select(4,'tb_member','nickname',array('#tb_member.tbid'=>'tb_reserve.member_id')).')[membername],tb_reserve.state,tb_curriculum_snap.type,tb_reserve.dt',$where);
				foreach($reserve as $v){
					$tmp['tbid']= $v['tbid'];
					$tmp['title'] = $v['title'];
					$tmp['intro'] = $v['intro'];
					//$tmp['coachname'] = $v['coachname'];
					
					$tmp['state'] = $v['state'];
					$tmp['curriculumtype']=$curriculumtype[$v['type']];
					$tmp['reservedt'] = date('Y-m-d',strtotime($v['dt']));
					$tmp['reservetime'] = date('H',strtotime($v['dt']));
					$week=date('w',strtotime($v['dt']));
					switch($week){
						case 0:$week='日';break;
						case 1:$week='一';break;
						case 2:$week='二';break;
						case 3:$week='三';break;
						case 4:$week='四';break;
						case 5:$week='五';break;
						case 6:$week='六';break;
					}
					$tmp['week'] = '星期'.$week;
					
					
					if($data['usertype']==2){
					    $tmp['coachname'] =$v['membername'];
					}else{
					    $sql = "select mobile,nickname from tb_coach where tbid=?";
					    $ht = $db2->prepare($sql);
					    $ht->execute(array($v['coachid']));
					    $tmp11 = $ht->fetchAll(PDO::FETCH_ASSOC);
					    $tmp['mobile'] = $tmp11[0]['mobile'];
					    $tmp['coachname'] = $tmp11[0]['nickname'];
					    
					    $tmp['headimg'] = file_exists('dofiles/coach/'.$v['coachid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/coach/'.$v['coachid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
					}
					
					
					$echo['data'][]=$tmp;

				}
				
				$sql = "select mobile from tb_member where tbid=?";
				$ht = $db2->prepare($sql);
				$ht->execute(array($data['userid']));
				$tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
				$echo['mobile'] = $tmp[0]['mobile'];
				
				
				$echo['headimg'] = file_exists('dofiles/member/'.$data['userid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/member/'.$data['userid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
				
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
